package com.mediator
{
	import com.build.ProtoBuilderContext;
	import com.common.EventManager;
	import com.common.Global;
	import com.common.GlobalEvent;
	import com.common.Mediator;
	import com.constant.LangType;
	import com.view.EditClassView;
	import com.view.ProtoConfigView;
	import com.vo.AttributeVo;
	import com.vo.ClassVo;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;

	/**
	 * @author lijia
	 */	
	public class ProtoConfigViewMediator extends Mediator
	{
		private var view:ProtoConfigView;

		public function ProtoConfigViewMediator(view:ProtoConfigView)
		{
			this.view = view;
			super(view);
		}

		public override function initListener():void{
			addEventListener(view.classNameInput, Event.CHANGE, searchHandler);
			
			addEventListener(view.compileBtn, MouseEvent.CLICK, onCompileClick);
			
			addEventListener(EventManager.instance, GlobalEvent.OPEN_PROTO_UPDATE_PANEL, onOpenProtoUpdatePanel);

//			addEventListener(view.addProtoBtn, MouseEvent.CLICK, onAddProtoClick);
			
			addEventListener(EventManager.instance, GlobalEvent.SAVE_PROTO_CONFIG, onSaveConfig);
			
//			addEventListener(view.saveXmlBtn, MouseEvent.CLICK, onSaveConfig);
			
			addEventListener(EventManager.instance, GlobalEvent.ADD_PROTO_CONFIG, onAddProtoConfig);
		}

		private function onAddProtoConfig(e:GlobalEvent):void{
			dataList ||= new ArrayCollection();
			dataList.addItem(e.obj);
			view.protoList.dataProvider = dataList;
			onSaveConfig();
		}

		private function onCompileClick(e:MouseEvent):void{
			var now:int = getTimer();
			for each(var clsVo:ClassVo in dataList){
				if(Global.isSelectAll || clsVo.isSelect){
//					Global.produceServerFile(clsVo.clone());
//					Global.produceClientFile(clsVo.clone());
					ProtoBuilderContext.getInstance().build(LangType.CSHARP, clsVo);
//					ProtoBuilderContext.getInstance().build(LangType.ERLANG, clsVo.clone());
				}
			}
//			Global.BuildCMD(dataList);
			
			Alert.show("编译完毕！耗时："+(getTimer()-now)+"毫秒");
		}

		private function onSaveConfig(e:Event = null):void{
			var now:int = getTimer();
			var xmlStr:String = new String("<root>\n");
			for each(var clsVo:ClassVo in dataList){
				xmlStr += "\t<proto name=\""+clsVo.className+"\">\n";
				for each(var attrVo:AttributeVo in clsVo.attributeList){
					xmlStr += "\t\t<attribute name=\""+attrVo.attrName+"\"";
					xmlStr += " type=\""+attrVo.attrType+"\"";
					if(attrVo.annotation && attrVo.annotation.length>0){
						xmlStr += " value=\""+attrVo.annotation+"\"";
					}
					xmlStr += "/>\n";
				}
				xmlStr += "\t</proto>\n";
			}
			xmlStr += "</root>";
			Global.writeFile(Global.ProtoProjectPath, "proto", xmlStr, "xml");
			view.protoList.dataProvider = dataList;
			Alert.show("保存XML完毕！耗时："+(getTimer()-now)+"毫秒");
		}

		private var editView:EditClassView;
		private function onOpenProtoUpdatePanel(e:GlobalEvent):void{
			editView ||= new EditClassView();
			if(editView.mediator){
				editView.mediator.vo = e.obj as ClassVo;
			}
			editView.vo = e.obj as ClassVo;
			PopUpManager.addPopUp(editView, this.view, true);
			PopUpManager.centerPopUp(editView);
		}
		
		private function onAddProtoClick(e:MouseEvent):void{
			editView ||= new EditClassView();
			if(editView.mediator){
				editView.mediator.vo = null;
			}
			editView.vo = null;
			PopUpManager.addPopUp(editView, this.view, true);
			PopUpManager.centerPopUp(editView);
		}

		protected function searchHandler(event:Event=null):void
		{
			var index:int=checkSource();
			view.protoList.ensureIndexIsVisible(index);
			view.protoList.selectedIndex = index;
		}
		
		private function checkSource():int{
			for (var i:int = 0; i < dataList.length; i++) 
			{
				var str:String = String(dataList[i].mid+dataList[i].className);
				var isMatch:Boolean=matchStr(str.toLocaleLowerCase(), view.classNameInput.text.toLocaleLowerCase());
				if(isMatch==true){
					return i;
				}
			}
			return 0;
		}

		/**
		 * 匹配选择
		 */
		private function matchStr(str:String,regExpStr:String):Boolean{
			//^匹配字符串的开头
			if(str.indexOf(regExpStr)==-1){
				return false;
			}else{
				return true;
			}
			return false;
		}

		private var dataList:ArrayCollection;
//		public function initProtoList(xml:XML):void{
//			dataList = new ArrayCollection();
//			for each(var proto:XML in xml.proto){
//				var cvo:ClassVo = new ClassVo();
//				cvo.className = proto.@name;
//				cvo.classType = proto.@type;
//				cvo.desc = proto.@desc;
//				cvo.mid = int(proto.@mid);
//				cvo.attributeList = new Vector.<AttributeVo>();
//				cvo.isSelect = false;
//				for each(var attr:XML in proto.attribute){
//					var pvo:AttributeVo = new AttributeVo();
//					pvo.attrName = attr.@name;
//					pvo.attrType = attr.@type;
//					pvo.annotation = attr.@desc;
//					cvo.attributeList.push(pvo);
//				}
//				dataList.addItem(cvo);
//			}
//			this.view.protoList.dataProvider = dataList;
//		}

	}
}